import {Message} from 'element-ui'
import NProgress from 'nprogress'
import axios from 'axios'
// import Cookies from 'js-cookie'

axios.defaults.baseURL = process.env.VUE_APP_HOME_API_URL
axios.defaults.timeout = 30 * 1000 // 设置接口响应时间

// let token = Cookies.get('token')
let token = 'swagger-ui'

/* URL地址token白名单 */
const URL_WHITE_TOKEN_LIST = [
  '/admin/account/login'
]

/**
 * 功能描述：Http Request 拦截器
 */
axios.interceptors.request.use((request) => {
  NProgress.start() // 展示进度条

  /* 当前请求地址不是白名单，则增加请求头token */
  if (URL_WHITE_TOKEN_LIST.indexOf(request.url) === -1) {
    request.headers.common['Authorization'] = `Bearer ${token}`
  }
  request.headers.common['Content-Type'] = 'application/json;charset=UTF-8'

  return request
}, (error) => { // 请求错误时做些事(接口错误、超时等)
  NProgress.done() // 隐藏进度条
  Message.error(`请求参数错误`, 10)

  /* 1.判断请求超时 */
  if (error.code === 'ECONNABORTED' && error.message.indexOf('timeout') !== -1) {
    // return service.request(originalRequest);//例如再重复请求一次
  }

  /* 2.需要重定向到错误页面 */
  if (error.response) {
    // error =errorInfo.data//页面那边catch的时候就能拿到详细的错误信息,看最下边的Promise.reject
    // router.push({
    //   path: `/error/${errorInfo.status}`// 404 403 500 ... 等
    // })
  }

  return Promise.reject(error) // 在调用的那边可以拿到(catch)你想返回的错误信息
})

/**
 * 功能描述：Http Response 拦截器
 */
axios.interceptors.response.use((res) => {
  NProgress.done() // 隐藏进度条

  /* 判断当前请求响应是Excel导出的，直接返回 */
  if (res.config.url.indexOf('/export-excel') >= 0) {
    return Promise.resolve(res)
  }

  let isTips = res.config.showMessage === undefined || res.config.showMessage === true
  let data = res.data

  /* 根据返回的code值来做不同的处理（和后端约定） */
  switch (data.code) {
    case 200:
      return Promise.resolve(data)
    default:
      isTips && Message.error(`服务器返回异常：${data.message}`, 10)
      return Promise.reject(res)
  }
}, (error) => {
  Message.error(`服务器返回异常：${error}`, 10)

  if (error && error.response) {
    switch (error.response.status) {
      case 400:
        break
      default:
        break
    }
  }

  return Promise.reject(error)
})

export default {
  get (url, data = {}, config = {}) {
    return new Promise((resolve, reject) => {
      axios.get(url, data, config).then(response => {
        resolve(response)
      }).catch(err => {
        reject(err)
      })
    })
  },
  delete (url, data = {}, config = {}) {
    return new Promise((resolve, reject) => {
      axios.delete(url, data, config).then(response => {
        resolve(response)
      }).catch(err => {
        reject(err)
      })
    })
  },
  post (url, data = {}, config = {}) {
    return new Promise((resolve, reject) => {
      axios.post(url, data, config).then(response => {
        resolve(response)
      }, err => {
        reject(err)
      })
    })
  },
  put (url, data = {}, config = {}) {
    return new Promise((resolve, reject) => {
      axios.put(url, data, config).then(response => {
        resolve(response)
      }, err => {
        reject(err)
      })
    })
  },
  patch (url, data = {}, config = {}) {
    return new Promise((resolve, reject) => {
      axios.patch(url, data, config).then(response => {
        resolve(response)
      }, err => {
        reject(err)
      })
    })
  }
}
